<?php

class FP_Image_MultiLineOverlay extends FP_Image {

  const LEFT = 'left';
  const CENTER = 'center';
  const RIGHT = 'right';

  protected $text = null;
  protected $angle = null;
  protected $font_url = null;
  protected $font_size = 10;
  protected $leading = 0;
  protected $justify = self::LEFT;
  protected $foreground_color = array(255,255,255);
  protected $text_x = 0;
  protected $text_y = 0;
  protected $padding = array('t' => 0, 'r' => 0, 'b' => 0, 'l' => 0);
  protected $truncate_at = 0;

  public function __construct($pBaseFile, $pText, $pFontUrl, array $pOptions=array(), $pDestination=null, $pMimetype=null){

    $this->create($pBaseFile);

    $this->width = imageSX($this->image);
    $this->height= imageSY($this->image);

    $this->text = $pText;
    $this->font_url = $pFontUrl;

    if (array_key_exists('font_size', $pOptions))
      $this->font_size = $pOptions['font_size'];

    if (array_key_exists('justify', $pOptions))
      $this->justify = $pOptions['justify'];

    if (array_key_exists('leading', $pOptions))
      $this->leading = $pOptions['leading'];

    if (array_key_exists('font_size', $pOptions))
      $this->font_size = $pOptions['font_size'];

    if (array_key_exists('foreground_color', $pOptions))
      $this->foreground_color = $pOptions['foreground_color'];

    if (array_key_exists('truncate', $pOptions))
      $this->truncate_at = $pOptions['truncate'];


    if (array_key_exists('padding', $pOptions)){
      $padding = $pOptions['padding'];
      if (array_key_exists('t', $padding))
	$this->padding['t'] = $padding['t'];
      if (array_key_exists('r', $padding))
	$this->padding['r'] = $padding['r'];
      if (array_key_exists('b', $padding))
	$this->padding['b'] = $padding['b'];
      if (array_key_exists('l', $padding))
	$this->padding['l'] = $padding['l'];
    }

    if (array_key_exists('angle', $pOptions))
      $this->angle = $pOptions['angle'];

    $test_text = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_';
    $_globalbox = imageTTFBbox($this->font_size, 0, $this->font_url, $test_text);
    $globalbox = convertBoundingBox($_globalbox);

    $width = $this->width;
    $height = $this->height;

    $text_color = imagecolorallocate($this->image, $this->foreground_color[0],$this->foreground_color[1],$this->foreground_color[2]);

    if ($this->justify == self::LEFT){
      if ($this->truncate_at > 0)
	$text = truncate($this->text, $this->truncate_at);
      else
	$text = $this->text;

      $text_x = $this->text_x + $this->padding['l'];
      imagettftext($this->image, $this->font_size, $this->angle, $text_x, $this->font_size, $text_color, $this->font_url, $text);
    } else {
      $strings = preg_split('/\n/', $this->text);
      $__H = $this->padding['t'];

      if ($this->justify == self::RIGHT) {

	foreach ($strings as $key => $val){
	  if ($this->truncate_at > 0)
	    $text = truncate($val, $this->truncate_at);
	  else
	    $text = $val;

	  $_b = imageTTFBbox($this->font_size, 0, $this->font_url, $text);
	  $box = convertBoundingBox($_b);

	  $_W = $box['width'];

	  $_X = $width - $_W - $this->padding['r'];

	  $_H = $globalbox['height'];
	  $__H += $_H + $this->leading;

	  imagettftext($this->image, $this->font_size, $this->angle, $_X, $__H, $text_color, $this->font_url, $text);
	}
      } else if ($this->justify == self::CENTER){

	foreach ($strings as $key => $val){
	  if ($this->truncate_at > 0)
	    $text = truncate($val, $this->truncate_at);
	  else
	    $text = $val;

	  $_b = imageTTFBbox($this->font_size, 0, $this->font_url, $text);
	  $box = convertBoundingBox($_b);

	  $_W = $box['width'];

	  $_X = abs($width / 2) - abs($_W / 2);

	  $_H = $globalbox['height'];

	  $__H += $_H;
	  imagettftext($this->image, $this->font_size, $this->angle, $_X, $__H, $text_color, $this->font_url, $text);
	  $__H += $this->leading;
	}
      }
    }

    if ($pDestination)
      $this->save($pDestination);

  }

}

?>